################################################
## DESCRIPTION:
##   Converts dpcuments from WORD format to 
##   final release format (either RTF or PDF)
##
## Legal Notices:
##   Copyright (c) 2008, Telliam Consulting, LLC.
##   All rights reserved.
##
##   Redistribution and use in source and binary forms, with or without modification,
##   are permitted provided that the following conditions are met:
##
##   * Redistributions of source code must retain the above copyright notice, this list
##     of conditions and the following disclaimer.
##   * Redistributions in binary form must reproduce the above copyright notice, this
##     list of conditions and the following disclaimer in the documentation and/or other
##     materials provided with the distribution.
##   * Neither the name of Telliam Consulting nor the names of its contributors may be
##     used to endorse or promote products derived from this software without specific
##     prior written permission.
##
##   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
##   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
##   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
##   SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
##   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
##   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
##   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
##   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
##   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
##   DAMAGE. 
##
$scriptroot = (Get-Location -PSProvider FileSystem).ProviderPath
$ConvertedDocs = "$ScriptRoot\ConvertedDocs"
$RawDocs = "$ScriptRoot\End User Docs"

function global:Save-WordDocAs($App, [string]$SrcDoc, [string]$DstDoc, [string]$Format, [switch]$MarkVersion)
{
    if(!$App)
    {
        $App = new-object -com Word.Application
        $App.visible = $false
        $AppCreated=$true
    }
    else
    {
        $AppCreated=$false
    }
    $doc = $app.Documents.Open($SrcDoc)
    [void]($doc.Activate())
    if($MarkVersion)
    {
         $app.Selection.start = 0
         $app.Selection.End = 0         
         $Find = $app.Selection.Find
         $Find.ClearFormatting()
         [void]($Find.execute("<%BUILD_VER%>", $false, $false, $false, $false, $false, $true, 1, $false, $env:BUILD_VER, 2))
    }

    switch($Format.ToUpper())
    {
    "PDF" {
              ## only office 2007 has export to PDF support (used on build server for final output)
              if($App.Version -ge 12)
              {
                  $doc.ExportAsFixedFormat($DstDoc, 17)
              }
              else
              {
                  ## just copy the doc file (and force the doc extension) if Office 2007 not found
                  "WARNING: Office 2007 Not present copying $SrcDoc instead of converting it to PDF" 
                  $FormatID = 0
                  $doc.SaveAs([ref]$DstDoc, [ref]$FormatID)
              }
          }
    "RTF" {
              if($App.Version -ge 12)
              {
                  $FormatID = 6
              }
              else
              {
                  $FormatID = $App.fileconverters.Item("MSWord6RTFExp").SaveFormat
              }
              [void]($doc.SaveAs([ref]$DstDoc, [ref]$FormatID))
          }
    }
    [void]($doc.Close([ref]0))
    if($AppCreated)
    {
        [void]($App.Quit())
    }
}

$Word = new-object -com Word.Application

if(test-path "$ConvertedDocs")
{
    rd -r -force $ConvertedDocs    
}

[void](md "$ConvertedDocs")
[void](md "$ConvertedDocs\Common")

## release notes are always saved as RTF since (rare) user might not have PDF reader installed - it provides info on obtaining the reader
Save-WordDocAs -MarkVersion $Word "$RawDocs\Release Notes.doc" "$ConvertedDocs\Common\Release Notes.RTF" "RTF"

## Save other docs as PDF

## Export to PDF only supported on Office 2007
if($Word.Version -ge 12)
{
    ## All other docs converted to PDF (Note PDF conversion only supported on systems with office 2007)
    Save-WordDocAs $Word "$RawDocs\Eula.doc" "$ConvertedDocs\Common\Eula.PDF" "PDF"
}
else
{
    ## No PDF generation capability on earlier versions of office so just copy the "blank.PDF"
    ## this allows the rest of the build system to remain unaltered for dev machines that don't have Office 2007 
    
    ## ERROR if this is the build server!
    if($env:USERNAME -ieq "BuildServer") { throw "ERROR - BuildServer *MUST* support PDF generation!" }

    copy "$RawDocs\blank.pdf" "$ConvertedDocs\Common\Eula.PDF"
}

[void]($Word.Quit())